package com.example.objectstoragejava.web;

import com.example.objectstoragejava.entity.ObsPreSign;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.iam.v3.IamClient;
import com.huaweicloud.sdk.iam.v3.model.*;
import com.huaweicloud.sdk.iam.v3.region.IamRegion;
import com.obs.services.ObsClient;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.HttpMethodEnum;
import com.obs.services.model.TemporarySignatureRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "obs")
@CrossOrigin
public class ObsUpload {
    String ACCESS_KEY_ID = "WPLPUVCM3Q2KYPHTGEZM";
    String SECRET_ACCESS_KEY_ID = "3OyZSOhUyDif6cic8ZehwQUKYhUVULf2zEbVR3e1";
    String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";

    // 获取临时凭证
    @RequestMapping(value = "get-sts")
    public CreateTemporaryAccessKeyByTokenResponse upload() {
        ICredential auth = new GlobalCredentials()
                .withAk(ACCESS_KEY_ID)
                .withSk(SECRET_ACCESS_KEY_ID);

        IamClient client = IamClient.newBuilder()
                .withCredential(auth)
                .withRegion(IamRegion.valueOf("cn-east-3"))
                .build();
        CreateTemporaryAccessKeyByTokenRequest request = new CreateTemporaryAccessKeyByTokenRequest();
        CreateTemporaryAccessKeyByTokenRequestBody body = new CreateTemporaryAccessKeyByTokenRequestBody();
        List<String> listStatementAction = new ArrayList<>();
        listStatementAction.add("obs:*:*");
        List<ServiceStatement> listPolicyStatement = new ArrayList<>();
        listPolicyStatement.add(
                new ServiceStatement()
                        .withAction(listStatementAction)
                        .withEffect(ServiceStatement.EffectEnum.fromValue("Allow"))
        );
        ServicePolicy policyIdentity = new ServicePolicy();
        policyIdentity.withVersion("1.1")
                .withStatement(listPolicyStatement);
        IdentityToken tokenIdentity = new IdentityToken();
        tokenIdentity.withDurationSeconds(1200);
        List<TokenAuthIdentity.MethodsEnum> listIdentityMethods = new ArrayList<>();
        listIdentityMethods.add(TokenAuthIdentity.MethodsEnum.fromValue("token"));
        TokenAuthIdentity identityAuth = new TokenAuthIdentity();
        identityAuth.withMethods(listIdentityMethods)
                .withToken(tokenIdentity)
                .withPolicy(policyIdentity);
        TokenAuth authbody = new TokenAuth();
        authbody.withIdentity(identityAuth);
        body.withAuth(authbody);
        request.withBody(body);
        try {
            CreateTemporaryAccessKeyByTokenResponse response = client.createTemporaryAccessKeyByToken(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
